﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TuanAnhPersonalWeb.Models;

namespace TuanAnhPersonalWeb.Classes
{
    public class EmployeeAccessConsolidator
    {
        TuanAnhEntities db = new TuanAnhEntities();
        public IEnumerable<TUANANH__POSITION> getAllPosition()
        {
            var data = db.TUANANH__POSITION.Select(e => e).ToList();
            return data.Count() > 0 ? data : null;
        }
        public IList<EmployeeModel> getAllEmploy()
        {
            IList<EmployeeModel> list = new List<EmployeeModel>();
            var data = db.TUANANH__EMPLOYEE.ToList();
            foreach (var item in data)
            {
                EmployeeModel model = new EmployeeModel();
                model.EMP_ID = item.EMP_ID;
                model.EMP_AGE = item.EMP_AGE;
                model.EMP_IMAGE = item.EMP_IMAGE;
                model.EMP_INFO = item.EMP_INFO;
                model.EMP_NAME = item.EMP_NAME;
                model.EMP_TITLE = item.EMP_TITLE;
                model.POS_ID = item.POS_ID;
                model.ISHOMEPAGE = item.ISHOMEPAGE;
                model.POS_NAME = db.TUANANH__POSITION.Where(e => e.POS_ID == item.POS_ID).Select(e => e.POS_DES).Single();
                list.Add(model);
            }
            return list;
        }
        public EmployeeModel getEmployById(int id)
        {
            TUANANH__EMPLOYEE item = db.TUANANH__EMPLOYEE.Single(e => e.EMP_ID == id);
            EmployeeModel model = new EmployeeModel();
            model.EMP_ID = item.EMP_ID;
            model.EMP_AGE = item.EMP_AGE;
            model.EMP_IMAGE = item.EMP_IMAGE;
            model.EMP_INFO = item.EMP_INFO;
            model.EMP_NAME = item.EMP_NAME;
            model.EMP_TITLE = item.EMP_TITLE;
            model.ISHOMEPAGE = item.ISHOMEPAGE;
            model.POS_ID = item.POS_ID;
            model.POS_NAME = db.TUANANH__POSITION.Where(e => e.POS_ID == item.POS_ID).Select(e => e.POS_DES).Single();
            return model;

        }
        public void InsertEmploy(EmployeeModel item)
        {
            TUANANH__EMPLOYEE model = new TUANANH__EMPLOYEE();
            model.EMP_AGE = item.EMP_AGE;
            model.EMP_IMAGE = item.EMP_IMAGE;
            model.EMP_INFO = item.EMP_INFO;
            model.EMP_NAME = item.EMP_NAME;
            model.EMP_TITLE = item.EMP_TITLE;
            model.POS_ID = item.POS_ID;
            model.ISHOMEPAGE = false;
            db.TUANANH__EMPLOYEE.AddObject(model);
            db.SaveChanges();
              
        }
        public void UpdateEmploy(EmployeeModel item)
        {
            TUANANH__EMPLOYEE model = db.TUANANH__EMPLOYEE.Where(e => e.EMP_ID == item.EMP_ID).Single();
            model.EMP_AGE = item.EMP_AGE;
            model.EMP_IMAGE = item.EMP_IMAGE;
            model.EMP_INFO = item.EMP_INFO;
            model.EMP_NAME = item.EMP_NAME;
            model.EMP_TITLE = item.EMP_TITLE;
            model.POS_ID = item.POS_ID;
            db.SaveChanges();
        }
    }
}